﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Xml;
using System.Data;
//using fastJSON;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Diagnostics;
using System.Collections.ObjectModel;
//using Microsoft.Contracts

public partial class REST : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        this.Label1.Text = "";
        GridView1.DataSource = "";
        GridView1.DataBind();

        string URI;
        string response;
        //string parameters;
        
        URI = this.TextBox1.Text;

        WebRequest request = WebRequest.Create(URI);
        request.Method = "GET";
        request.ContentType = "application/json";
        //parameters = "{\"param1\":scottsmith\"}";
        //request.ContentLength = parameters.Length;

        //StreamWriter requestwriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
        //requestwriter.Write(parameters);
        //requestwriter.Close();
        
        StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
        response = responseReader.ReadToEnd();

        this.Label1.Text = response;
        responseReader.Close();

    }

    // http://msdn.microsoft.com/en-us/library/bb412179(v=VS.90).aspx
    protected void Button_DeserializeJson(object sender, EventArgs e)
    {

        this.Label1.Text = "";
        string URI;
        string response;

        URI = this.TextBox1.Text;

        WebRequest request = WebRequest.Create(URI);
        request.Method = "GET";
        request.ContentType = "application/json";

        StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
        response = responseReader.ReadToEnd();
        responseReader.Close();

        Collection<Account> thisAccount = new Collection<Account>();

        var serializer = new DataContractJsonSerializer(typeof(Collection<Account>));
        using (var tempStream = new MemoryStream(Encoding.Unicode.GetBytes(response)))
        {
            thisAccount = (Collection<Account>)serializer.ReadObject(tempStream);
        }

        GridView1.DataSource = thisAccount;

        GridView1.DataBind();

    }

    protected void Button3_Click(object sender, EventArgs e)
    {

        string URI;
        string response;

        URI = this.TextBox1.Text;

        WebRequest request = WebRequest.Create(URI);
        request.Method = "GET";
        request.ContentType = "application/json";

        StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
        response = responseReader.ReadToEnd();
        responseReader.Close();

        Collection<Account> thisAccount = new Collection<Account>();

        var serializer = new DataContractJsonSerializer(typeof(Collection<Account>));
        using (var tempStream = new MemoryStream(Encoding.Unicode.GetBytes(response)))
        {
            thisAccount = (Collection<Account>)serializer.ReadObject(tempStream);
        }

        GridView1.DataSource = thisAccount;

        GridView1.DataBind();


       
    }
   
       
    protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = "";
        GridView1.DataBind();

        this.TextBox1.Text = "";
        this.Label1.Text = "";

    }

    public class Account
    {
        private string _namenumber;

        public string name { get; set; }
        public string number { get; set; }
        //public string MessageValue { get; set; }
        public string namenumber
        {
            get { return name + " " + number; }
            set { _namenumber = value; }
        }
    }

    // got this here: http://stackoverflow.com/questions/3391104/way-to-deserialize-json-from-httpwebresponse-without-3rd-party-frameworks
    public static class JsonHelper
    {
        public static string ToJson<T>(T instance)
        {
            var serializer = new DataContractJsonSerializer(typeof(T));
            using (var tempStream = new MemoryStream())
            {
                serializer.WriteObject(tempStream, instance);
                return Encoding.Default.GetString(tempStream.ToArray());
            }
        }

        public static T FromJson<T>(string json)
        {
            var serializer = new DataContractJsonSerializer(typeof(T));
            using (var tempStream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                return (T)serializer.ReadObject(tempStream);
            }
        }
    }

}